//
//  TableViewPersonCell.swift
//  movieInfo
//
//  Created by zhangkai on 2023/5/13.
//

import UIKit

class TableViewPersonCell: UITableViewCell,UICollectionViewDelegate, UICollectionViewDataSource {


    var two:[Cast] = []
    var callBlock:((_ model:Cast)->Void)?
    
    func configData(datas:[Cast]){
        self.two = datas
        self.collectionView.reloadData()
    }
    
    private var collectionView: UICollectionView!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        flowLayout.itemSize = CGSize(width: 80, height: 80)
        flowLayout.scrollDirection = .horizontal
        
        collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TableViewPersonsubCell.self, forCellWithReuseIdentifier: "TableViewPersonsubCell")
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.two.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TableViewPersonsubCell", for: indexPath) as? TableViewPersonsubCell ?? TableViewPersonsubCell()
        
//        cell.backgroundColor = .darkText
        
        
        if let path = self.two[indexPath.row].profilePath {
            cell.img.downloadImage(fromPath: path)
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        callBlock?(self.two[indexPath.row])
    }
    

}


class TableViewPersonsubCell:UICollectionViewCell{
    var img = MIImageView(frame: CGRect(x: 0, y: 0, width: 70, height: 70))
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(img)
        img.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(70)
        }
        img.layer.cornerRadius = 35
        img.layer.masksToBounds = true
        img.contentMode = .scaleAspectFill
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
